<template>
	<view class="bg-white">
		<cola-chat
			@refresh="refreshNow"
			:isUseRefresh="isUseRefresh"
			userIdName="userId"
			:userId="user.id"
			:historyMsg="historyMsg"
			:updateMsg="updateData"
			:messageData="msgData"
			:isUseRead="true"
			:dataKeys="dataKey"
			IDType="self_msg_id"
			@sendMsg="send"
			@btnPhoto="btnPhoto"
			@refreshBtn="refreshBtn"
		></cola-chat>
	</view>
</template>

<script>
import colaChat from '@/subPages/my/list/card/component/cola-u-chat/cola-u-chat.vue';
import { businessAddChat, businessChatList } from '@/api/my/card.js';
import { baseUrl } from '@/util/api.js';
import { dateTimeStr } from '@/common/js/util.js';
export default {
	components: {
		'cola-chat': colaChat
	},
	data() {
		return {
			user: {},
			friend: {},
			dataKey: {
				userId: 'self_user_uid',
				msgId: 'self_msg_id',
				// name: 'user_name',
				message: 'msg',
				avator: 'avator',
				img: 'img',
				read: 'read',
				time: 'add_time'
			},
			msgData: [],
			historyMsg: [],
			updateData: {},
			page: 1,
			isUseRefresh: true,
			refresh: false //是否刷新最新消息
		};
	},
	onLoad(option) {
		// 好友
		this.friend = JSON.parse(option.friend);
		// console.log(this.friend);
		uni.setNavigationBarTitle({
			title: this.friend.name
		});
		// 我的
		this.user = uni.getStorageSync('userMsg');
		// console.log('user', this.user);

		uni.setStorageSync('productType', 'qz');
	},
	onShow() {
		// wx.hideHomeButton()
	},
	beforeCreate() {},
	mounted() {
		this.getHistory(1);
	},
	beforeDestroy() {
		// console.log('准备销毁了')
	},
	onBackPress(options) {
		// console.log(options);
	},
	methods: {
		// 历史记录
		getHistory(page) {
			let msg = {
				self_user_uid: this.user.id,
				give_user_uid: this.friend.uid,
				page: page,
				pagesize: 30
			};
			businessChatList(msg).then(res => {
				console.log('res历史记录', res);
				if (res.code == 0) {
					if (this.refresh) {
						console.log("11");
						uni.hideLoading();
						if (res.data.data.length < 1) {
							uni.$u.toast('当前已是最新内容');
						} else {
							this.msgData = res.data.data;
							console.log(this.msgData);
						}
					} else {
						console.log("22");
						this.historyMsg = res.data.data;
						if (this.historyMsg <= 0) {
							setTimeout(() => {
								this.isUseRefresh = false;
							}, 1000);
						}
					}
				}
			});
		},
		sendCont(val, type) {
			this.updateData = {
				self_user_uid: this.user.id,
				give_user_uid: this.friend.uid,
				self_msg_id: Math.random().toFixed(4) * 10000,
				// user_name: this.user.nickname,
				msg: type == 'text' ? val : '',
				img: type == 'img' ? val : '',
				avator: this.user.userphoto,
				read: false,
				add_time: dateTimeStr('y-m-d h:i:s')
			};
		},
		sendApi(val) {
			businessAddChat(val).then(res => {
				// console.log(res);
			});
		},
		// 发送消息
		async send(e) {
			await this.sendCont(e, 'text');
			// console.log(this.updateData);
			this.sendApi(this.updateData);
		},
		// 发送照片
		btnPhoto() {
			uni.chooseImage({
				success: chooseImageRes => {
					const tempFilePaths = chooseImageRes.tempFilePaths;
					uni.uploadFile({
						url: baseUrl + '/model/common/layer_upload_img',
						filePath: tempFilePaths[0],
						name: 'file',
						// formData: e,
						success: async res => {
							if (res.statusCode == 200) {
								let msg = JSON.parse(res.data);
								// console.log('上传图片', msg);
								await this.sendCont(msg.img, 'img');
								this.sendApi(this.updateData);
							} else {
								uni.$u.toast('图片上传失败');
							}
						}
					});
				}
			});
		},
		refreshNow(e) {
			// console.log(e);
			this.getHistory(++this.page);
		},
		// 刷新--获取新数据
		refreshBtn() {
			this.refresh = true;
			uni.showLoading({
				title: '正在加载最新消息...'
			});
			setTimeout(() => {
				this.historyMsg = [];
				this.getHistory(0);
			}, 1000);
		}
	}
};
</script>

<style></style>
